import java.io.File;
import java.util.Locale;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableHyperlink;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class ResultWriter {
	private File excel;
	private WritableSheet excelSheet;
	private WritableWorkbook workbook;
	private WritableCellFormat fmt_ok, fmt_fail;
	private boolean transpose = false;

	public ResultWriter(String filename) {
		// open excel output file
		excel = new File(filename);
		WorkbookSettings wbSettings = new WorkbookSettings();

		wbSettings.setLocale(new Locale("en", "EN"));

		try {
			workbook = Workbook.createWorkbook(excel, wbSettings);
		} catch (Exception e) {
			System.out.println("[Error] " + e.toString());
			return;
		}
		workbook.createSheet("Report", 0);
		excelSheet = workbook.getSheet(0);

		fmt_ok = new WritableCellFormat();
		fmt_fail = new WritableCellFormat();
		try {
			fmt_ok.setBackground(Colour.GREEN);
			fmt_ok.setBorder(Border.ALL, BorderLineStyle.MEDIUM,
					Colour.DARK_GREEN);
			fmt_fail.setBackground(Colour.RED);
			fmt_fail.setBorder(Border.ALL, BorderLineStyle.MEDIUM,
					Colour.DARK_RED);
		} catch (Exception e) {
			System.out.println("[Error] " + e.toString());
		}

	}

	public void transposeSheet(boolean transpose) {
		this.transpose = transpose;
	}

	public void addLabel(int col, int row, String text) {
		// write header to excel file
		Label label = new Label(transpose ? row : col, transpose ? col : row,
				text);
		try {
			excelSheet.addCell(label);
		} catch (Exception e) {
			System.out.println("[Error] " + e.toString());
		}
	}

	public void addNumber(int col, int row, Integer integer) {
		// write header to excel file
		Number number = new Number(transpose ? row : col,
				transpose ? col : row, integer);
		try {
			excelSheet.addCell(number);
		} catch (Exception e) {
			System.out.println("[Error] " + e.toString());
		}
	}

	public void addOk(int col, int row, String msg) {
		try {
			excelSheet.addCell(new Label(transpose ? row : col, transpose ? col
					: row, msg, fmt_ok));
		} catch (Exception e) {
			System.out.println("[Error] " + e.toString());
		}
	}

	public void addOk(int col, int row) {
		addOk(col, row, " ");
	}

	public void addFail(int col, int row, String err) {
		try {
			excelSheet.addCell(new Label(transpose ? row : col, transpose ? col
					: row, err, fmt_fail));
		} catch (Exception e) {
			System.out.println("[Error] " + e.toString());
		}
	}

	public void addLink(int col, int row, String link, String descr) {
		try {
			WritableHyperlink h = new WritableHyperlink(transpose ? row : col,
					transpose ? col : row, new File(link), descr);
			excelSheet.addHyperlink(h);
		} catch (Exception e) {
			System.out.println("[Error] " + e.toString());
		}
	}

	public void addFail(int col, int row) {
		addFail(col, row, " ");
	}

	public void close() {
		// write excel file
		try {
			workbook.write();
			workbook.close();
		} catch (Exception e) {
			System.out.println("[Error] " + e.toString());
			return;
		}
	}
}
